<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport"
	content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<meta name="description" content="">
<meta name="author" content="ThemeBucket">
<link rel="shortcut icon" type="image/x-icon"
	href="${ctxPath}/static/images/lukk.ico" media="screen" />
<link rel="icon" href="${ctxPath}/static/images/lukk.ico"
	type="image/x-icon" />
<link rel="bookmark" href="${ctxPath}/static/images/lukk.ico"
	type="image/x-icon" />
<title>管理页面</title>

<!-- tables插件的引用完成-->


</head>
<body class="sticky-header"
	onload="indexmenu('${oneMenu}','${twoMenu}');">
	<%@ include file="../index/index.jsp"%>
	<section>
		<div class="main-content" style="overflow-y: auto">
			<!-- 地址栏-->
			<div class="page-heading">
				<h3>鹿客帮</h3>
				<ul class="breadcrumb">
					<li><a>意向师傅 </a></li>
					<li class="active">查看我的录入</li>
				</ul>
			</div>

			<div class="panel-body" style="padding-bottom: 0px;">
				<div class="panel panel-default">
					<div class="panel-heading">查询条件</div>
					<div class="panel-body">
						<form id="formSearch" class="form-horizontal">
							<div class="form-group" style="margin-top: 15px">
								<label class="control-label col-sm-1"
									for="txt_search_workerName">师傅名称</label>
								<div class="col-sm-2">
									<input type="text" class="form-control"
										id="txt_search_workerName">
								</div>
								<label class="control-label col-sm-1"
									for="txt_search_phoneNumber">师傅手机</label>
								<div class="col-sm-2">
									<input type="text" class="form-control"
										id="txt_search_phoneNumber">
								</div>
								<label class="control-label col-sm-1"
									for="txt_search_phoneNumber">意向状态</label>
								<div class="col-sm-2">
									<select class="form-control" name="potentialLevelID"
										id="potentialLevelSelect">
									</select>
								</div>
								<div class="col-sm-2" style="text-align: left;">
									<button type="button" style="margin-left: 50px" id="btn_query"
										class="btn btn-primary">查询</button>
								</div>
							</div>
						</form>
					</div>
				</div>

				<div id="toolbar" class="btn-group">
					<button id="btn_edit" type="button" class="btn btn-default">
						<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
					</button>
					<button id="btn_giveUp" type="button" class="btn btn-default">
						<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>放弃
					</button>
					<button type="button" onclick="sendMessageToWorkers()"
						class="btn btn-default">
						<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>群发短信
					</button>
					<label> &nbsp;&nbsp;&nbsp;&nbsp;注释：放弃用户不在页面上显示，但是也不能再次添加。</label>
				</div>
				<table id="tb_potentialWorker"></table>
			</div>

		</div>
	</section>
	<!--  修改数据模拟狂  -->
	<div class="modal fade" style="top: 200px;"
		id="modifyPotentialWorkerModal" tabindex="-1" role="dialog"
		aria-labelledby="modifyPotentialWorkerModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="modifyPotentialWorkerModalLabel">管理潜在员工模拟框</h4>
				</div>
				<div class="modal-body">
					<form class="form-horizontal" id="modifyPotentialWorkerForm"
						role="form">
						<input type="hidden" name="id" id="txt_workerID">
						<div class="form-group">
							<label for="lastname" class="col-sm-2 control-label">师傅名称</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="txt_workerName"
									name="workerName" placeholder="师傅的名称">
							</div>
						</div>
						<div class="form-group">
							<label for="lastname" class="col-sm-2 control-label">师傅手机</label>
							<div class="col-sm-10">
								<input type="text" class="form-control" id="txt_phoneNumber"
									name="phoneNumber" placeholder="师傅的手机">
							</div>
						</div>
					</form>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭
					</button>
					<button type="button" onclick="modifyPotentialWorker()"
						class="btn btn-primary">提交更改</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal -->
	</div>
</body>


<!-- tables插件的引用 -->
<script src="${ctxPath}/static/js/bootstrap-table/bootstrap-table.js"></script>
<link href="${ctxPath}/static/js/bootstrap-table/bootstrap-table.css"
	rel="stylesheet" />
<script
	src="${ctxPath}/static/js/bootstrap-table/locale/bootstrap-table-zh-CN.js"></script>

<script type="text/javascript">
	//修改师傅的信息
	function modifyPotentialWorker() {
		var phone = $("#txt_phoneNumber").val();
		if (phone && /^1[3|4|5|8]\d{9}$/.test(phone)) {
			$.post("${ctxPath}/potentialWorker/modify", $(
					"#modifyPotentialWorkerForm").serialize(), function(data) {
				if (data) {
					$("#modifyPotentialWorkerModal").modal('hide')
					$('#modifyPotentialWorkerForm')[0].reset();
					$("#tb_potentialWorker").bootstrapTable('refresh');
					toastr.success('已经修改成功了');
				} else {
					toastr.info('不可修改为该手机号,该手机号已经存在了');
				}
			});
		} else {
			toastr.error("手机号码不可使用");
		}

	}
	//获取所有的选中的checkbox
	function getALLCheckedCheckboxs() {
		var arrselections = $("#tb_potentialWorker").bootstrapTable(
				'getSelections');
		//获取所有的状态
		var alreayCheckboxs = "";
		$(arrselections).each(function(i) {
			alreayCheckboxs += ("," + this.id);
		});
		return alreayCheckboxs.substring(1);
	}
	//给单个工人发送短信
	function sendMessageToWorker(workerID) {
		Ewin.confirm({
			message : "你确定要给他发送短信吗？"
		}).on(function(e) {
			if (!e) {
				return;
			} else {
				$.post('${ctxPath}/potentialWorker/sendMessage', {
					workers : workerID.toString()
				}, function(data) {
					if (data) {
						toastr.error("已经给选择人员发送了！");
					}
				});
			}
		});
	}
	//给选中工人发送短信
	function sendMessageToWorkers() {
		Ewin.confirm({
			message : "你确定要给他们发送短信吗？"
		}).on(
				function(e) {
					if (!e) {
						return;
					} else {
						var arrselections = $("#tb_potentialWorker")
								.bootstrapTable('getSelections');
						if (arrselections.length > 0) {
							$.post('${ctxPath}/potentialWorker/sendMessage', {
								workers : getALLCheckedCheckboxs()
							}, function(data) {
								if (data) {
									toastr.success("已经给选择人员发送了！");
								}
							});
						} else {
							toastr.error("请选择有效数据！");
						}
					}
				});

	}
	$(function() {
		$.post('${ctxPath}/potentialLevel/select', function(data) {
			$(data)
					.each(
							function(index, currData) {
								$("#potentialLevelSelect").append(
										"<option value="+currData.id+">"
												+ currData.levelCode + "--("
												+ currData.levelContent
												+ ")</option>");
							});
			//1.初始化Table
			var oTable = new TableInit();
			oTable.Init();
			//2.初始化Button的点击事件
			var oButtonInit = new ButtonInit();
			oButtonInit.Init();
		});
	});
	var TableInit = function() {
		var oTableInit = new Object();
		//初始化Table
		oTableInit.Init = function() {
			$('#tb_potentialWorker')
					.bootstrapTable(
							{
								url : '${ctxPath}/potentialWorker/queryByPage', //请求后台的URL（*）
								method : 'post', //请求方式（*）
								toolbar : '#toolbar', //工具按钮用哪个容器
								striped : true, //是否显示行间隔色
								cache : false, //是否使用缓存，默认为true，所以一般情况下需要设置一下这个属性（*）
								pagination : true, //是否显示分页（*）
								sortable : false, //是否启用排序
								sortName : 'createTime',
								sortOrder : "desc", //排序方式
								queryParams : oTableInit.queryParams,//传递参数（*）
								sidePagination : "server", //分页方式：client客户端分页，server服务端分页（*）
								pageNumber : 1, //初始化加载第一页，默认第一页
								pageSize : 8, //每页的记录行数（*）
								pageList : [ 8, 25, 50, 100 ],
								search : false, //是否显示表格搜索，此搜索是客户端搜索，不会进服务端，所以，个人感觉意义不大
								strictSearch : false,
								showColumns : false, //是否显示所有的列
								showRefresh : true, //是否显示刷新按钮
								minimumCountColumns : 2, //最少允许的列数
								clickToSelect : false, //是否启用点击选中行
								height : 600, //行高，如果没有设置height属性，表格自动根据记录条数觉得表格高度
								uniqueId : "ID", //每一行的唯一标识，一般为主键列
								showToggle : true, //是否显示详细视图和列表视图的切换按钮
								cardView : false, //是否显示详细视图
								detailView : false, //是否显示父子表
								contentType : 'application/x-www-form-urlencoded; charset=UTF-8',
								columns : [
										{
											checkbox : true,
											width : '5%'
										},
										{
											field : 'workerName',
											title : '师傅名称',
											width : '10%',
											formatter : function(value, row,
													index) {
												if (value == '未设置') {
													return '<font color="red">'
															+ value + '</font>'
												}
												return value;
											}
										},
										{
											field : 'phoneNumber',
											title : '师傅手机',
											width : '15%'
										},
										{
											field : 'createTime',
											title : '创建时间',
											width : '15%'
										},
										{
											field : 'status',
											title : '目前状态',
											width : '10%',
											formatter : function(value, row,
													index) {
												if (value == 0) {
													return '未注册';
												} else if (value == 1) {
													return '<font color="green">已注册</font>';
												} else {
													return '<font color="red">未通过</font>';
												}
											}
										},
										{
											field : 'potentialLevel',
											title : '意向程度',
											width : '15%',
											formatter : function(value, row,
													index) {
												return value.levelCode + "--("
														+ value.levelContent
														+ ")";
											}
										},
										{
											field : 'tag',
											title : '发送短信',
											width : '15%',
											formatter : function(value, row,
													index) {
												return "<button class='btn btn-info btn-xs' onclick='javascript:sendMessageToWorker("
														+ row.id
														+ ")'>发送短信</button>";
											}
										}, ]
							});
		};

		//得到查询的参数
		oTableInit.queryParams = function(params) {
			var temp = { //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
				limit : params.limit, //页面大小
				offset : params.offset, //页码
				order : params.order,
				sort : params.sort,
				workerName : $("#txt_search_workerName").val(),
				phoneNumber : $("#txt_search_phoneNumber").val(),
				potentialLevelID : $("#potentialLevelSelect").find(
						"option:selected").val()
			};
			return temp;
		};
		return oTableInit;
	};

	var ButtonInit = function() {
		var oInit = new Object();
		var postdata = {};

		oInit.Init = function() {
			$("#btn_edit")
					.click(
							function() {
								var arrselections = $("#tb_potentialWorker")
										.bootstrapTable('getSelections');
								if (arrselections.length > 1) {
									toastr.warning('只能选择一行进行编辑');
									return;
								}
								if (arrselections.length <= 0) {
									toastr.warning('请选择有效数据');
									return;
								}
								$("#modifyPotentialWorkerModalLabel").text(
										"编辑师傅的信息");
								$("#txt_workerID").val(arrselections[0].id);
								$("#txt_workerName").val(
										arrselections[0].workerName);
								$("#txt_phoneNumber").val(
										arrselections[0].phoneNumber);
								$('#modifyPotentialWorkerModal').modal();
							});

			$("#btn_giveUp")
					.click(
							function() {
								var arrselections = $("#tb_potentialWorker")
										.bootstrapTable('getSelections');
								if (arrselections.length <= 0) {
									toastr.warning('请选择有效数据');
									return;
								}
								Ewin
										.confirm({
											message : "&nbsp;您确定放弃掉选中的号码吗？ "
										})
										.on(
												function(e) {
													if (!e) {
														return;
													} else {
														$
																.post(
																		'${ctxPath}/potentialWorker/drop',
																		{
																			drops : getALLCheckedCheckboxs()
																		},
																		function(
																				data) {
																			if (data) {
																				$(
																						"#tb_potentialWorker")
																						.bootstrapTable(
																								'refresh');
																				toastr
																						.success('该用户再也不会出现了');
																			}
																		});
														//发送删除数据
													}
												});

							});

			$("#btn_query").click(function() {
				//	$("#tb_potentialWorker").bootstrapTable('removeAll');
				var opt = {
					
					pageNumber : 1,
					query : {
						offset : 0
					}
				};
				//从新开始计数
				$("#tb_potentialWorker").bootstrapTable('refresh', opt);
			});
		};
		return oInit;
	};
</script>
</html>
